package com.lb.shopguide.util.pic;

import android.content.Context;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.lb.shopguide.GuideApplication;
import com.lb.shopguide.R;

import java.io.File;


/**
 * author：zhangxy
 * time：2016/12/1  13:55.
 * descr：图片加载工具类 使用glide框架封装
 */

public class ImageLoaderUtils {

    public static void displayGif(int drawableId, ImageView imageView) {
        getGlideManager().asGif().load(drawableId).into(imageView);
    }


    public static void display(Context context, ImageView imageView, String url) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .error(R.drawable.iv_empty)
                .placeholder(R.drawable.iv_image_loading)
                .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        getGlideManager().load(url).apply(options).into(imageView);
    }

    public static void display(Context context, ImageView imageView, File url) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .error(R.drawable.iv_empty)
                .placeholder(R.drawable.iv_image_loading)
                .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        getGlideManager().load(url).apply(options).into(imageView);
    }

    public static void displaySmallPhoto(Context context, ImageView imageView, String url) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .error(R.drawable.iv_empty)
                .placeholder(R.drawable.iv_image_loading)
                .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        getGlideManager().asBitmap().load(url).apply(options).thumbnail(0.5f).into(imageView);
    }

    public static void displayBigPhoto(Context context, ImageView imageView, String url) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .format(DecodeFormat.PREFER_ARGB_8888)
                .error(R.drawable.iv_empty)
                .placeholder(R.drawable.iv_image_loading)
                .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        getGlideManager().asBitmap().load(url).apply(options).into(imageView);
    }

    public static void display(Context context, ImageView imageView, int url) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .error(R.drawable.iv_empty)
                .placeholder(R.drawable.iv_image_loading)
                .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        getGlideManager().load(url).apply(options).into(imageView);
    }

    public static void displayCircle(Context context, ImageView imageView, String url) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .error(R.drawable.iv_default_avatar)
                .transform(new GlideCircleTransfromUtil(context))
                .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        getGlideManager().load(url).apply(options).into(imageView);
    }

    public static void displayRound(Context context, ImageView imageView, String url, int dp) {
        if (imageView == null) {
            throw new IllegalArgumentException("argument error");
        }
        getGlideManager().load(url).apply(getSGCommonGlideOption(dp)).into(imageView);
    }

    public static RequestOptions getSGCommonGlideOption(int radius) {
        return new RequestOptions()
                .centerCrop()
                .error(R.drawable.iv_default_avatar)
                .transform(new GlideRoundTransformUtil(GuideApplication.getAppContext(), radius))
                .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
    }

    public static RequestManager getGlideManager() {
        return Glide.with(GuideApplication.getAppContext());
    }
}
